﻿// Decompiled with JetBrains decompiler
// Type: Microsoft.InfoCards.RpcCryptoContext
// Assembly: infocard, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 8E14765A-6610-409A-BA36-099A0642905D
// Assembly location: E:\git\ALLIDA\windll\infocard.exe

using Microsoft.InfoCards.Diagnostics;
using System;
using System.ComponentModel;

namespace Microsoft.InfoCards
{
  internal class RpcCryptoContext : IDisposable
  {
    private string m_portName;
    private string m_contextKey;
    private IntPtr m_idlHandle;
    private object m_syncRoot;
    private bool m_open;
    private bool m_disposed;

    public RpcCryptoContext(string portName, string contextKey)
    {
      this.m_portName = portName;
      this.m_contextKey = contextKey;
      this.m_syncRoot = new object();
      this.m_disposed = false;
    }

    ~RpcCryptoContext()
    {
      if (this.m_disposed)
        return;
      ((IDisposable) this).Dispose();
    }

    public string ContextKey
    {
      get
      {
        return this.m_contextKey;
      }
    }

    public IntPtr InterfaceHandle
    {
      get
      {
        return this.m_idlHandle;
      }
    }

    public bool IsOpen
    {
      get
      {
        return this.m_open;
      }
    }

    public void Open()
    {
      this.ThrowIfDisposed();
      if (this.m_open)
        return;
      try
      {
        this.m_idlHandle = NativeMcppMethods.RpcCryptoOpen(this.m_portName);
      }
      catch (Win32Exception ex)
      {
        throw InfoCardTrace.ThrowHelperError((Exception) new CommunicationException((string) null, (Exception) ex));
      }
      this.m_open = true;
    }

    public void Close()
    {
      if (!this.m_open)
        return;
      try
      {
        NativeMcppMethods.RpcCryptoClose(this.m_idlHandle, this.m_contextKey);
      }
      catch (Win32Exception ex)
      {
        throw InfoCardTrace.ThrowHelperError((Exception) new CommunicationException((string) null, (Exception) ex));
      }
      this.m_idlHandle = IntPtr.Zero;
      this.m_open = false;
    }

    void IDisposable.Dispose()
    {
      if (this.m_disposed)
        return;
      lock (this.m_syncRoot)
      {
        if (this.m_disposed)
          return;
        this.m_disposed = true;
        try
        {
          this.Close();
        }
        catch (CommunicationException ex)
        {
          string message = ex.Message;
        }
        this.m_portName = (string) null;
        this.m_contextKey = (string) null;
        this.m_idlHandle = IntPtr.Zero;
        GC.SuppressFinalize((object) this);
      }
    }

    private void ThrowIfDisposed()
    {
      if (this.m_disposed)
        throw InfoCardTrace.ThrowHelperError((Exception) new ObjectDisposedException(nameof (RpcCryptoContext)));
    }
  }
}
